#!/usr/bin/env python
# -*- coding: utf-8 -*-

from usermanagement.forms import LoginForm

def loginform(request):
    """
    This is a custom context preprocessor
    It is required to to fill the login_form.html template
    which is iclude in the basic index.html template.
    Futhermore the current URL is included as a next parameter in every 
    login form. This enables the loginview to redirect users back to 
    the page from where they used the loginform after the sucessfull login.
    If the user is already logged in, a welcome message and the logout link
    are displayed.
    For more information on contextpreprocessors see :
    http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext
    """
    if 'next' in request.GET:           #required to handle the 
            next = request.GET['next']  #@login_required decorator
    else: 
            if request.path == "/users/login/" :
                next = "/"
            else : 
                next = request.path

    if request.user.is_authenticated():
        return {'current_user' : request.user}
    else:
        login_form = LoginForm({'next' : next})
        return {'login_form' : login_form,
                'current_user' : False }
